package com.badwei.ydd.controller;

import com.alibaba.fastjson.JSON;
import com.badwei.ydd.pojo.IpAddress;
import com.badwei.ydd.pojo.Message;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: badwei
 * @Date: 2022/5/10 22:55
 * @Description:
 */
@RestController
@CrossOrigin
public class TestController {



    @RequestMapping("/test")
    public String test(@RequestHeader("User-Agent") String user_agent,
                       @RequestHeader Map<String,String> header){

        HashMap<String, Object> map = new HashMap<>();
        map.put("user_agent在这:",user_agent);
        map.put("header在这:",header);
        return header.get("host");

//        //解析agent字符串
//        UserAgent userAgent = UserAgent.parseUserAgentString(user_agent);
////获取浏览器对象
//        Browser browser = userAgent.getBrowser();
////获取操作系统对象
//        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
//        System.out.println("浏览器名:"+browser.getName());
////        System.out.println("浏览器类型:"+browser.getBrowserType());
////        System.out.println("浏览器家族:"+browser.getGroup());
////        System.out.println("浏览器生产厂商:"+browser.getManufacturer());
////        System.out.println("浏览器使用的渲染引擎:"+browser.getRenderingEngine());
//        System.out.println("浏览器版本:"+userAgent.getBrowserVersion());
//
//
//
//        System.out.println("操作系统名:"+operatingSystem.getName());
////        System.out.println("访问设备类型:"+operatingSystem.getDeviceType());
////        System.out.println("操作系统家族:"+operatingSystem.getGroup());
////        System.out.println("操作系统生产厂商:"+operatingSystem.getManufacturer());
//
//        return map.toString();
    }

    @RequestMapping("/test2/{ip}")
    public String test2(@PathVariable("ip") String ip) throws UnsupportedEncodingException {
        RestTemplate restTemplate=new RestTemplate();
        String url = "http://api.map.baidu.com/location/ip?ak=oNwrkbQhQNj9cKSSGUExG6LVAS4Dvryf&ip="+ ip +"&coor=bd09ll";
        IpAddress  result = restTemplate.getForObject(url, IpAddress.class);
        if (ObjectUtils.isEmpty(result)){
            return null;
        }
        String address = result.getContent().getAddress();
        return URLDecoder.decode(address,"UTF-8");

    }
}
